const express = require('express')
const mysql = require('mysql')
const bodyParser = require('body-parser')

const app = express()
// 一定要在路由之前，配置 cors 这个中间件，从而解决接口跨域的问题
const cors = require('cors')
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
let timestamp = new Date().getTime()



// 1. 导入路由模块
const authorize = require('./util/authorization')
const router_user = require('./server_module/user')
const router_book = require('./server_module/book')
const router_upload = require('./server_module/upload')
const router_static = require('./server_module/static')

app.all('*', function (req, res, next) {
    timestamp = new Date().getTime()
    res.header("Access-Control-Allow-Origin", "*");
    // res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    // res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    // res.header('if-none-match', 'no-match-for-this');req.params['0']

    // let authorizePath = authorize.authorizePath(req._parsedUrl.pathname)
    let token = authorize.verificationToken(req.headers.token,req._parsedUrl.pathname)
    if (!token) {
        res.send({
            msg: '重新登录',
            code: 501,
            timestamp
        })
        return
    }

    next();
});

app.use('/api',router_user)
app.use('/api',router_book)
app.use('/api',router_upload)
app.use('/api',router_static)




app.listen(3000, () => console.log('服务器已就绪'))